package com.example.psysystemhomework;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.psysystemhomework.bean.StoryBean;
import com.example.psysystemhomework.util.AppManager;
import com.example.psysystemhomework.util.NetUtil;
import com.example.psysystemhomework.util.ShareUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

public class UploadActivity extends AppCompatActivity {

    private EditText mTitle;
    private EditText mContent;
    private Button mBtUpload;
    private Context c;
    private ShareUtil shareUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload);
        initViews();
        initEdit();
        initData();
    }

    private void initEdit() {
        shareUtil = new ShareUtil(this);
        if (shareUtil.getStoryDraft() != null) {
            mTitle.setText(shareUtil.getStoryDraft().getType());
            mContent.setText(shareUtil.getStoryDraft().getContent());
        }
    }

    private void initData() {
        c = this;
        EventBus.getDefault().register(this);
        mTitle.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {


            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String s = editable.toString();


            }
        });

        mContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                shareUtil.saveContent(mTitle.getText().toString(), editable.toString(), false);
            }
        });


        mBtUpload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mContent.getText().toString().isEmpty() || mContent.getText().toString().isEmpty()) {
                    Toast.makeText(c, "请输入内容", Toast.LENGTH_SHORT).show();


                }

                if (mContent.getText().length() < 30) {
                    Toast.makeText(c, "内容必须大于30个字符", Toast.LENGTH_SHORT).show();
                    Log.i("内容限制日志", "onClick: 内容限制");
                } else {
                    uploadData();
                    view.setEnabled(false);


                }


            }
        });
    }

    private void uploadData() {
        Log.i("上传故事请求网络", "run: 网络接口被定义");
        ShareUtil shareUtil = new ShareUtil(this);
        StoryBean storyBean = new StoryBean();
        if (shareUtil.getUser() != null) {
            storyBean.setUserId(shareUtil.getUser().getUserId());
            storyBean.setContent(mContent.getText().toString().trim());
            storyBean.setType(mTitle.getText().toString().trim());

        } else {
            Toast.makeText(this, "请先登录", Toast.LENGTH_SHORT).show();
        }
        new Thread() {
            @Override
            public void run() {
                NetUtil netUtil = new NetUtil(getApplicationContext());

                JSONObject jsonObject = netUtil.uploadStory(storyBean);
                Log.i("上传故事情况展示", "run: " + jsonObject.toString());
                EventBus.getDefault().post(jsonObject);
                super.run();
            }
        }.start();
    }


    @Override
    protected void onDestroy() {
        AppManager appManager = AppManager.getAppManager();
        appManager.finishActivity();
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    private void initViews() {
        mTitle = findViewById(R.id.title);
        mContent = findViewById(R.id.content);
        mBtUpload = findViewById(R.id.bt_upload);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void upload(JSONObject jsonObject) {

        Toast.makeText(c, jsonObject.toString(), Toast.LENGTH_SHORT).show();



    }

}